package com.sharemarking.system.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sharemarking.system.entity.Permission;
import com.sharemarking.system.mapper.PermissionMapper;

@Controller
@RequestMapping("system/permission")
public class PermissionController {
	
	@Autowired
	private PermissionMapper pm;
	
	/**
	 * 权限列表页面
	 * @param search
	 * @param model
	 */
	@GetMapping("index")
	@PreAuthorize("hasPermission('base','all')")
	public void index(Model model,String targetDomain){
		List<Permission>list=pm.getAll();
		Set<String> domain=new HashSet<String>();
		for(Permission p:list)domain.add(p.getDomain());
		if(targetDomain==null)targetDomain=domain.size()>0?domain.iterator().next():null;
		model.addAttribute("targetDomain", targetDomain);
		model.addAttribute("domains", domain);
		model.addAttribute("permissions", list);
	}
	/**
	 * 新增用户页面
	 * @param obj
	 * @param model
	 * @return
	 */
	@GetMapping("0")
	@PreAuthorize("hasPermission('base','all')")
	public String addPage(Model model){
		model.addAttribute("permissions", new ArrayList<Permission>());
		return "system/permission/form";
	}
	/**
	 * 编辑用户页面
	 * @param obj
	 * @param model
	 * @return
	 */
	@GetMapping("{domain}")
	@PreAuthorize("hasPermission('base','all')")
	public String get(@PathVariable("domain")String domain,Model model){
		List<Permission> list=pm.getAll();
		List<Permission> targetList=list.stream().filter(p->p.getDomain().equals(domain)).collect(Collectors.toList());
		model.addAttribute("domain", domain);
		model.addAttribute("permissions", targetList);
		return "system/permission/form";
	}
	/**
	 * 新增权限
	 * @param target
	 * @param model
	 * @return
	 */
	@PostMapping("0")
	@PreAuthorize("hasPermission('base','all')")
	@Transactional
	public String add(DomainDto src){
		List<Permission> list=src.getPermissions();
		if(list!=null) {
			list.removeIf(p->StringUtils.isEmpty(p.getName()));
			list.forEach(p->{
				p.setDomain(src.getDomain());
				pm.insert(p);
			});
		}
		return "redirect:index?domain="+src.getDomain();
	}
	/**
	 * 修改权限
	 * @param target
	 * @param model
	 * @return
	 */
	@PostMapping("{prevDomain}")
	@PreAuthorize("hasPermission('base','all')")
	@Transactional
	public String update(@PathVariable("prevDomain")String prevDomain,DomainDto src){
		if(src.getPermissions()!=null){
			src.getPermissions().removeIf(p->StringUtils.isEmpty(p.getName()));
			src.getPermissions().forEach(p->{
				p.setDomain(src.getDomain());
				if(p.getId()!=null){
					pm.updateInfo(p);
				}else{
					pm.insert(p);
				}
				
				
			});
		}
		return "redirect:index?domain="+src.getDomain();
	}
	/**
	 * 删除权限
	 * @param id
	 * @return
	 */
	@GetMapping("{domain}/delete")
	@PreAuthorize("hasPermission('base','all')")
	@Transactional
	public String delete(@PathVariable("domain")String domain){
		pm.deleteByDomain(domain);
		return "redirect:../index";
	}
	
	public static class DomainDto{
		private String domain;
		private List<Permission> permissions;
		public String getDomain() {
			return domain;
		}
		public void setDomain(String domain) {
			this.domain = domain;
		}
		public List<Permission> getPermissions() {
			return permissions;
		}
		public void setPermissions(List<Permission> permissions) {
			this.permissions = permissions;
		}
	}
	
}
